﻿var siteBaseAddress = "/"; //站点根目录
var cookieDomain = ".safetree.com.cn";//cookie域
var CSEE = {
    //退出
    loginOut: function (callback) {
        $.getJSON(siteBaseAddress + "Main/LoginOut.aspx", { r: Math.random() }, function () {
            store.clear("user");
            if (callback) {
                callback();
            }
            else {
                location.reload();
            }
        })
    },
    //显示登录弹层
    showLoginDialog: function () {
        var loginDialog = '\
          <div class="logintc" id="logintc" style="display:none;">\
          <iframe style="background-color:transparent; position:absolute; visibility:inherit;z-index:-1;" frameborder=0></iframe>\
          <a class="closebtn" href="javascript:void(0)" onClick="hide(\'logintc\').remove();">&times;</a>\
          <div class="logintc-box">\
            <div class="passworderror" style="display:none;" id="loginErrorInfo"></div>\
            <dl><h3>请先登录</h3></dl>\
            <dl><div class="input_bga"><input id="loginname" class="inputa" type="text" placeholder="输入帐号"/></div></dl>\
            <dl><div class="input_bgb"><input id="password"  class="inputa" name="" type="password" placeholder="输入密码"/></div></dl>\
            <dl><p><span><a target="_blank" href="' + siteBaseAddress + 'Main/PasswordBack.aspx">忘记帐号？</a>&nbsp;&nbsp&nbsp;|</span><a href="' + siteBaseAddress + 'Main/PasswordBack.aspx" target="_blank">忘记密码？</a></p></dl>\
            <dl><a class="login_btn" href="javascript:CSEE.login()">登录</a></dl>\
          </div>\
        </div>';
        $("body").append(loginDialog);
        $("#logintc #password").keydown(function (e) {
            if (e.keyCode == 13) { 
                CSEE.login();
            }
        })
        show("logintc");
    },
    login: function (callback) {
        var loginname = $("#loginname").val();
        var password = $("#password").val();
        if ($.trim(loginname) == "") {
            $("#loginErrorInfo").html("请输入账号!");
            $("#loginErrorInfo").show();
            return false;
        }
        else if ($.trim(password) == "") {
            $("#loginErrorInfo").html("请输入密码!");
            $("#loginErrorInfo").show();
            return false;
        }

        $.getJSON("https://speciallogin.safetree.com.cn/SpecialLoginHandler.asmx/SpecialLogin?jsoncallback=?", { account: loginname, password: password, r: Math.random() }, function (res) {
            if (res != null && res.userid > 0) {
                if (callback)
                    callback(res);
                else
                    location.reload();
            }
            else {
                $("#loginErrorInfo").html('您输入的账户名和密码不匹配，请重新输入。或者您<a href="' + siteBaseAddress + 'Main/PasswordBack.aspx">忘记了密码</a>？');
                $("#loginErrorInfo").show();
                return false;
            }
        })
    },
    //加载头部登录信息
    loadUserInfo: function (callback) {
        var serverSide = CSEE.getCookie("ServerSide") != null ? CSEE.getCookie("ServerSide") : null;
        var userId = CSEE.getCookie("UserID");
        if (serverSide != null && serverSide != "-1" && serverSide != "" && userId != null) {
            var userJson = CSEE.getLocalCache("user");
            if (userJson != null && userJson.ukey == userId) {
                $("#loginLinkBtn").hide();
                $("#loginUserName").html(userJson.data.truename);
                $("#userSiteUrl").attr("href", userJson.data.baseurl + "/mainpage.html");
                $(".loginAfter").show();
                if (callback)
                    callback(userJson.data);
            }
            else {
                $.getJSON(serverSide + "/Education/Special.asmx/GetUserInfo?jsoncallback=?", { r: Math.random() }, function (json) {
                    if (json.userid >= 0) {
                        //设置本地缓存
                        CSEE.setLocalCache("user", { ukey: userId, data: json }, 3 * 60);

                        $("#loginLinkBtn").hide();
                        $("#loginUserName").html(json.truename);
                        $("#userSiteUrl").attr("href", json.baseurl + "/mainpage.html");
                        $(".loginAfter").show();
                    }
                    if (callback)
                        callback(json);
                });
            }
        }
    },
    //获取cookie
    getCookie: function (name) {
        var arr,
	    reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return null;
    },
    //设置cookie
    setCookie: function (c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";path=/;domain=" + cookieDomain + ";expires=" + exdate.toGMTString());
    },
    //删除cookie
    delCookie: function (name) {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = getCookie(name);
        if (cval != null) {
            document.cookie = name + "=" + cval + ";path=/;domain=" + cookieDomain + ";expires=" + exp.toGMTString();
        }
    },
    //设置本地缓存:键,值,有效分钟(本地所有缓存大小不能超过2M)
    setLocalCache: function (key, value, minute) {
        var ms = new Date().getTime() + minute * 60 * 1000;
        var cache = { data: value, expires: ms };
        try {
            store.set(key, cache);
        }
        catch (e) {
            if (e.name == "QuotaExceededError") {
                store.clearAll();
                store.set(key, cache);
            }
        }
    },
    //获取本地缓存
    getLocalCache: function (key) {
        var ms = new Date().getTime();
        var cache = store.get(key);
        if (cache != null) {
            if (ms < cache.expires) {
                return cache.data;
            }
            else {
                store.clear(key);
            }
        }
        return null;
    },
    //删除本地缓存
    delLocalCache: function (key) {
        store.clear(key);
    }
};

